﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;
using System.Text.RegularExpressions;
namespace PhongMachTu.GUI
{
    public partial class DSBenhNhan : Form
    {
        public DSBenhNhan()
        {
            InitializeComponent();
        }
        int SoNguoi;
        int soBenhNhanToiDa;
        int thamSoID = 2;
        int strMaKhamBenh;
        int strMaBenhNhan = 0;
        KhambenhBLL bllKhamBenh = new KhambenhBLL();
        ThamSoBLL bllThamSo = new ThamSoBLL();
        KhambenhBLL KB = new KhambenhBLL();
        KHAMBENH kb = new KHAMBENH();
        ThuocBLL bllThuoc = new ThuocBLL();
        DonViBLL bllDonVi = new DonViBLL();
        CachDungBLL bllCachDung = new CachDungBLL();
        LoaibenhBLL bllLoaiBenh = new LoaibenhBLL();
        BenhNhanBLL bllBenhNhan = new BenhNhanBLL();
        ChiTietKhamBenhBLL bllCTKhamBenh = new ChiTietKhamBenhBLL();



        int tienthuoc = 0;
        int tienKham = 0;
        int tongTien = 0;
        private void DSBenhNhan_Load(object sender, EventArgs e)
        {

            try
            {
                EndablePKB(false);
                dgvDSBenhNhan.DataSource = bllKhamBenh.LayDSKhamBenh(DateTime.Parse(dtHomNay.Value.ToShortDateString()));
                if (bllThamSo.LayDSThamSo().Count > 0)
                {
                    foreach (var item in bllThamSo.LayDSThamSo())
                    {
                        thamSoID = item.ThamSoID;
                    }

                }
                soBenhNhanToiDa = bllThamSo.soBenhNhanToiDa();
                SoNguoi = dgvDSBenhNhan.Rows.Count;

                cbo_maLoaiBenh.DataSource = bllLoaiBenh.LayDSLoaiBenh();
                cbo_maLoaiBenh.DisplayMember = "TenLoaiBenh";
                cbo_maLoaiBenh.ValueMember = "MaLoaiBenh";
                cbo_maLoaiBenh.SelectedIndex = -1;


            }
            catch
            {


            }
        }
        void frm_Button_Clicked(object sender, EventArgs e)
        {
            DSBenhNhan_Load(sender, e);
        }
        private void btnThemBN_Click(object sender, EventArgs e)
        {
            GUI.frmThemBenhNhan frm = new frmThemBenhNhan();
            frm.SoNguoi = dgvDSBenhNhan.Rows.Count;
            frm.soBenhNhanToiDa = bllThamSo.soBenhNhanToiDa();
            frm.Button_Clicked += new EventHandler(frm_Button_Clicked);
            frm.ShowDialog();
        }

        private void btnSuaBN_Click(object sender, EventArgs e)
        {
            if (strMaBenhNhan != 0)
            {
                GUI.frmSuaBenhNhan frm = new frmSuaBenhNhan();
                frm.maBenhNhan = strMaBenhNhan;
                frm.Button_Clicked += new EventHandler(frm_Button_Clicked);
                frm.ShowDialog();

            }
            else
            {
                MsgBox.Error("Chưa chọn bệnh nhân !");
            }

        }

        private void btnXoaBN_Click(object sender, EventArgs e)
        {
            if (strMaBenhNhan != 0)
            {
                DialogResult dialogResult = MsgBox.YesNo(string.Format("Bạn có đồng ý xoá bệnh nhân {0}", txtTenBN.Text));
                if (dialogResult == DialogResult.Yes)
                {
                    if (bllBenhNhan.xoaBenhNhan(strMaBenhNhan))
                    {
                        DSBenhNhan_Load(sender, e);
                        clearTextbox();
                    }
                    else
                    {
                        MsgBox.Error("Xảy ra lỗi !");
                    }

                }

            }
            else
            {
                MsgBox.Error("Chưa chọn bệnh nhân !");
            }

        }
        public void clearTextbox()
        {
            txtTenBN.ResetText();
            txtDiaChi.ResetText();
            txtNgaySinh.ResetText();
            lblGT.ResetText();
            txt_TrieuChung.ResetText();
        }
        private void btnRefesh_Click(object sender, EventArgs e)
        {
            DSBenhNhan_Load(sender, e);
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {

                Regex regex = new Regex("^[\\d]+$");
                if (regex.IsMatch(txtTimKiem.Text))
                {
                    dgvDSBenhNhan.DataSource = bllKhamBenh.timKiemDS(Convert.ToInt32(txtTimKiem.Text));
                }
                else
                {
                    dgvDSBenhNhan.DataSource = bllKhamBenh.timKiemDS(txtTimKiem.Text);
                }

            }
            catch (Exception ex)
            {

                MsgBox.Error(ex);
            }

        }

        public void EndablePKB(bool ok)
        {
            cbo_maLoaiBenh.Enabled = ok;
            txt_TrieuChung.Enabled = ok;
            btnLuuPKB.Enabled = ok;
            dgvThuoc.Enabled = ok;
        }
        private void dgvDSBenhNhan_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtTenBN.Text = dgvDSBenhNhan.Rows[e.RowIndex].Cells["HoTen"].Value.ToString();
                txtDiaChi.Text = dgvDSBenhNhan.Rows[e.RowIndex].Cells["DiaChi"].Value.ToString();
                txtNgaySinh.Text = DateTime.Parse(dgvDSBenhNhan.Rows[e.RowIndex].Cells["NgaySinh"].Value.ToString()).ToShortDateString();
                lblGT.Text = "Giới Tính : " + dgvDSBenhNhan.Rows[e.RowIndex].Cells["GioiTinh"].Value.ToString();
                strMaBenhNhan = Int32.Parse(dgvDSBenhNhan.Rows[e.RowIndex].Cells["MaBenhNhan"].Value.ToString());
                strMaKhamBenh = Int32.Parse(dgvDSBenhNhan.Rows[e.RowIndex].Cells["MaKhamBenh"].Value.ToString());
                EndablePKB(true);
                LayPKB(strMaKhamBenh, strMaBenhNhan);
                if (bllCTKhamBenh.LayDSKhamBenh(strMaKhamBenh).Count > 0)
                {
                    dgvThuoc.DataSource = bllCTKhamBenh.LayDSCTKB(strMaKhamBenh);
                    tinhTien();
                }
            }
            catch
            {


            }
        }
        public void LayPKB(int makb, int maBN)
        {
            foreach (var item in bllKhamBenh.LayDSKhamBenh(makb, maBN))
            {
                if (item.MaLoaiBenh != null)
                {
                    cbo_maLoaiBenh.SelectedValue = item.MaLoaiBenh;

                }
                if (item.TrieuChung != null)
                {
                    txt_TrieuChung.Text = item.TrieuChung;
                }
            }
        }
        CHITIETKHAMBENH ct = new CHITIETKHAMBENH();

        private void btnLuuPKB_Click(object sender, EventArgs e)
        {
            try
            {
                KHAMBENH kb = new KHAMBENH();
                kb.MaBenhNhan = strMaBenhNhan;
                kb.MaLoaiBenh = cbo_maLoaiBenh.SelectedValue.ToString();
                kb.TrieuChung = txt_TrieuChung.Text;
                kb.MaKhamBenh = strMaKhamBenh;
                if (MsgBox.checkNull(cbo_maLoaiBenh))
                {
                    return;
                }
                if (MsgBox.checkNull(txt_TrieuChung))
                {
                    return;
                }
                if (KB.suaKhamBenh(kb))
                {
                    MsgBox.Success("Lập phiếu khám bệnh thành công !");
                    tienKham = bllThamSo.TienKham();
                    txtTienKham.Text = string.Format("{0:0,00}", tienKham);
                }
                else
                {
                    MsgBox.Error("Xẩy ra lỗi!");
                }
            }
            catch
            {


            }

        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            frmThanhToan frm = new frmThanhToan();
            MsgBox.hd.MaBenhNhan = strMaBenhNhan;
            MsgBox.hd.MaKhamBenh = strMaKhamBenh;
            MsgBox.hd.NgayKham = DateTime.Now;
            MsgBox.hd.TienThuoc = tienthuoc;
            MsgBox.hd.TienKham = tienKham;
            MsgBox.hd.TongTien = tongTien;
            frm.Button_Clicked += new EventHandler(frmPKB_Button_Clicked);
            frm.ShowDialog();
        }
        void frmPKB_Button_Clicked(object sender, EventArgs e)
        {
            Report.frmPhieuKhamBenh frmTT = new Report.frmPhieuKhamBenh();
            frmTT.maKhamBenh = strMaKhamBenh;
            frmTT.hoTen = txtTenBN.Text;
            frmTT.trieuChung = txt_TrieuChung.Text;
            frmTT.loaiBenh = cbo_maLoaiBenh.Text;
            frmTT.tienKham = tienKham.ToString();
            frmTT.tienThuoc = tienthuoc.ToString();
            frmTT.tongTien = tongTien.ToString();
            frmTT.Show();
        }

        public void tinhTien()
        {
            tienthuoc = bllCTKhamBenh.tinhTienThuoc(strMaKhamBenh);
            txtTienThuoc.Text = string.Format("{0:0,00}", tienthuoc);
            tienKham = bllThamSo.TienKham();
            txtTienKham.Text = string.Format("{0:0,00}", tienKham);
            tongTien = tienthuoc + tienKham;
            txtTongTien.Text = string.Format("{0:0,00}", tongTien);
        }
        void frm_ButtonIn_Clicked(object sender, EventArgs e)
        {
            Report.frmPhieuKhamBenh frmTT = new Report.frmPhieuKhamBenh();
            frmTT.maKhamBenh = MsgBox.hd.MaKhamBenh;
            frmTT.hoTen = txtTenBN.Text;
            frmTT.trieuChung = txt_TrieuChung.Text;
            frmTT.loaiBenh = cbo_maLoaiBenh.Text;
            frmTT.tienKham = tienKham.ToString();
            frmTT.tienThuoc = tienthuoc.ToString();
            frmTT.tongTien = tongTien.ToString();
            frmTT.Show();
        }

        private void dtHomNay_ValueChanged(object sender, EventArgs e)
        {
            DSBenhNhan_Load(sender, e);
        }
        private void btnThemThuoc_Click(object sender, EventArgs e)
        {
            GUI.frmThemThuoc frm = new frmThemThuoc();
            frm.strMaKhamBenh = strMaKhamBenh;
            frm.ButtonCT_Clicked += new EventHandler(frmCT_Button_Clicked);
            frm.ShowDialog();
        }
        void frmCT_Button_Clicked(object sender, EventArgs e)
        {
            dgvThuoc.DataSource = bllCTKhamBenh.LayDSCTKB(strMaKhamBenh);
            tinhTien();
        }

        private void btnSuaThuoc_Click(object sender, EventArgs e)
        {
            if (MsgBox.ctkb.MaThuoc!= null)
            {
                GUI.SuaThuoc frm = new SuaThuoc();
                frm.ButtonCT_Clicked += new EventHandler(frmCT_Button_Clicked);
                frm.ShowDialog();    
            }

        }

        private void dgvThuoc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvThuoc.Rows.Count > 0)
            {
                MsgBox.ctkb.MaKhamBenh = strMaKhamBenh;
                MsgBox.ctkb.MaThuoc = dgvThuoc.Rows[e.RowIndex].Cells[0].Value.ToString();
                MsgBox.ctkb.MaDonVi = dgvThuoc.Rows[e.RowIndex].Cells[1].Value.ToString();
            }
        }

        private void btnXoaThuoc_Click(object sender, EventArgs e)
        {
            if (MsgBox.ctkb.MaThuoc == null)
            {
                MsgBox.Info("Vui lòng chọn thuốc cần xoá");

            }
            else
            {
                DialogResult dialogResult = MsgBox.YesNo(string.Format("Bạn có đồng ý xoá Thuốc {0}", MsgBox.ctkb.MaThuoc));
                if (dialogResult == DialogResult.Yes)
                {
                    if (bllCTKhamBenh.xoaChiTietKhamBenh(MsgBox.ctkb.MaKhamBenh.Value, MsgBox.ctkb.MaThuoc, MsgBox.ctkb.MaDonVi))
                    {
                        dgvThuoc.DataSource = bllCTKhamBenh.LayDSCTKB(strMaKhamBenh);
                        tinhTien();
                    }
                    else
                    {
                        MsgBox.Error("Xảy ra lỗi !");
                    }
                }
            }
        }

        private void dgvThuoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            GUI.SuaThuoc frm = new SuaThuoc();
            frm.ButtonCT_Clicked += new EventHandler(frmCT_Button_Clicked);
            frm.ShowDialog();
        }

        private void btnRefeshThuoc_Click(object sender, EventArgs e)
        {
            dgvThuoc.DataSource = bllCTKhamBenh.LayDSCTKB(strMaKhamBenh);
            tinhTien();
        }

    }
}
